<template>
    <view>
        <!-- pages/address/list.wxml -->
        <!-- <header name="地址列表" type="1" isback="true" bind:back="back"></header> -->
        <!-- 没有地址 start -->
        <!-- <view class="wxaddress" @tap="selectwxaddress">
            <view>
                <image src="/static/wxaddress.png"></image>
                <text>获取微信收货地址</text>
            </view>
            <image src="/static/next.png"></image>
        </view> -->
        <view class="line"></view>
        <view class="dizhi-mywk" v-if="!list || list.length < 1">
            <image src="/static/noaddr.png"></image>
            <text>暂无地址哦~</text>
        </view>
        <!-- 没有地址 end -->

        <view>
            <!-- 地址列表 start -->

            <view class="dizhi-lbwk" :data-index="index" @tap="goselect" v-for="(item, index) in list" :key="index">
                <view class="dizhi-lbnr">
                    <view class="dizhi-xmdz">
                        <view class="dizhi-xqwk">
                            <view class="dizhi-xmdh">
                                <text class="name">{{ item.name }}</text>
                                <text class="phone">{{ item.phone }}</text>
                                <text class="moren" v-if="item.isdefault == '1' ? true : false">默认</text>
                            </view>
                            <view class="dizhi-dznr">{{ item.address }}</view>
                        </view>
                    </view>
                    <navigator class="dizhi-qwbj" v-if="isselect != 1" hover-class="none" @tap.stop.prevent="edit" :data-id="item.id">编辑</navigator>
                    <navigator class="dizhi-qwbj" v-if="isselect != 1" hover-class="none" @tap.stop.prevent="del" :data-id="item.id" style="color: #ff3c26">删除</navigator>
                </view>
            </view>
            <!-- 地址列表 end -->
        </view>

        <view class="addrbtn" @tap="addaddr">添加地址</view>
    </view>
</template>

<script>
// pages/ucenter/address.js
var app = getApp();
export default {
    components: {
    },
    data() {
        return {
            list: '',
            nextp: 1,
            isselect: '',
            type: '',
            addr: '',
            flag: '',
            noaddr: 0
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.isselect= options.isselect,
        this.type= options.type
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.nextp= 1
        this.getaddr();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (!this.nextp) {
        } else {
            this.getaddr();
        }
    },
    methods: {
        addrFun() {
            uni.navigateTo({
                url: '/pages/address/add'
            });
        },

        goselect(e) {
            var _this = this;

            var index = e.currentTarget.dataset.index;
            var data = this.list[index];

            if (this.isselect) {
                var pages = getCurrentPages();
                var prevpages = pages[pages.length - 2];
                prevpages.addr = data;
                uni.navigateBack({
                    delta: 1
                });
            }
        },

        selectwxaddress() {
            uni.chooseAddress({
                success: (result) => {
                    app.globalData.http(
                        'center/addaddr',
                        {
                            name: result.userName,
                            phone: result.telNumber,
                            sheng: result.provinceName,
                            shi: result.cityName,
                            qu: result.countyName,
                            address: result.detailInfo,
                            isdefault: 0,
                            citycode: result.postalCode
                        },
                        function (res) {}
                    );
                }
            });
        },

        edit(e) {
            var id = e.currentTarget.dataset.id;
            uni.navigateTo({
                url: '/pages/address/add?id=' + id
            });
        },

        del(e) {
            var _this = this;

            var id = e.currentTarget.dataset.id;
            uni.showModal({
                title: app.globalData.appname,
                content: '是否要删除该地址？',

                success(res) {
                    if (res.confirm) {
                        _this.$http(
                            'app/center/deladdr',
                            {
                                id: id
                            },
                            function (res) {
                                if (res.code == 0) {
                                    _this.nextp= 1
                                    _this.getaddr();
                                }
                            }
                        );
                    } else if (res.cancel) {
                    }
                }
            });
        },

        getaddr() {
            var _this = this;

            _this.$http(
                'center/addresslist',
                {
                    p: this.nextp
                },
                function (res) {
                    if (_this.nextp == 1 || _this.nextp == '') {
                        if (res.list) {
                            _this.list= res.data.list
                        } else {
                            _this.list= res.data.list
                        }
                    } else {
                        if (res.list) {
                            for (var i = 0; i < res.data.list.length; i++) {
                                _this.list.push(res.data.list[i]);
                            }
            
                        } 
                    }
            
                    _this.nextp= res.nextp
                
                }
            );
        },

        back() {
            if (!this.list) {
                var pages = getCurrentPages();
                var prevpages = pages[pages.length - 2];
                prevpages.setData({
                    addr: '',
                    flag: '1',
                    noaddr: 1
                });
                uni.navigateBack({
                    delta: 1
                });
            } else {
                uni.navigateBack({
                    delta: 1
                });
            }
        },
				addaddr(){
					    uni.navigateTo({
					      url: '/pages/address/add',
					    })
				}
    }
};
</script>
<style lang="scss"> 
@import './list.scss';
</style>
